﻿#include "SnowWolf.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"
#include "../MapObject.h"
#include "../../Settings.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    SnowWolf::SnowWolf(MonsterInfo *info) : MonsterObject(info)
    {
    }

    void SnowWolf::Attack()
    {

        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        if (getEnvir()->Random->Next(5) > 0)
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);
            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                return;
            }

            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 350, {getTarget(), damage, DefenceType::ACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            S::ObjectAttack *tempVar2 = new S::ObjectAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            tempVar2->Type = 1;
            Broadcast(tempVar2);
            LineAttack(1);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }

        if (getTarget()->Dead)
        {
            FindTarget();
        }

    }

    void SnowWolf::LineAttack(int distance)
    {
        std::vector<MapObject*> targets = FindAllTargets(distance, getCurrentLocation());
        if (targets.empty())
        {
            return;
        }

        for (int i = 0; i < targets.size(); i++)
        {
            AttackTime = getEnvir()->getTime() + AttackSpeed + 300;
            int damage = GetAttackPower(Stats[Stat::MinMC], Stats[Stat::MaxMAC]);
            if (damage == 0)
            {
                return;
            }

            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 450, {getTarget(), damage, DefenceType::ACAgility});
            ActionList.push_back(action);

            if (getEnvir()->Random->Next(Settings::PoisonResistWeight) >= getTarget()->Stats[Stat::PoisonResist])
            {
                if (getEnvir()->Random->Next(4) == 0)
                {
                    Poison *tempVar = new Poison(), *this;
                    tempVar->Owner = this;
                    tempVar->Duration = 5;
                    tempVar->PType = PoisonType::Slow;
                    tempVar->Value = GetAttackPower(Stats[Stat::MinSC], Stats[Stat::MaxSC]);
                    tempVar->TickSpeed = 1000;
                    getTarget()->ApplyPoison(tempVar, this);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                }
                if (getEnvir()->Random->Next(8) == 0)
                {
                    Poison *tempVar2 = new Poison(), *this;
                    tempVar2->Owner = this;
                    tempVar2->Duration = 5;
                    tempVar2->PType = PoisonType::Frozen;
                    tempVar2->Value = GetAttackPower(Stats[Stat::MinSC], Stats[Stat::MaxSC]);
                    tempVar2->TickSpeed = 1000;
                    getTarget()->ApplyPoison(tempVar2, this);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
                }
            }

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
        }

    }
}
